﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pharmacy.Data;
using Pharmacy.Data.Repositories;
using System.Text;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Data.Entity;
using Pharmacy.Ultilities;


namespace Pharmacy.Business.Login
{
    public partial class LoginBO: BaseBusiness
    {
        #region Constructors
        public LoginBO(IObjectContext objectContext) : base(objectContext){ }
        public LoginBO() : base() { }
        #endregion

        #region Methods
        public User ValidateUserName(string userName, string password, out StringBuilder errorMessage)
        {
            errorMessage = new StringBuilder();
            password = CryptHelper.Encrypt(password.Trim());
            UserRepository rUser = new UserRepository(PharmacyContext);
            User existedUserName = rUser.AsQueryable().Where(u => u.UserName == userName).FirstOrDefault();
            if (existedUserName == null)
                errorMessage.AppendLine(String.Format(SR.Instance.GetString(Constant.KEY_INVALIDINFORMATION), SR.Instance.GetString(Constant.KEY_USERNAMETEXT)));
            existedUserName = rUser.AsQueryable().Where(u => u.Password == password).FirstOrDefault();
            if (existedUserName == null)
                errorMessage.AppendLine(String.Format(SR.Instance.GetString(Constant.KEY_INVALIDINFORMATION), SR.Instance.GetString(Constant.KEY_PASSWORDTEXT)));
            return existedUserName;
        }        
        #endregion
    }
}
